<template>
  <div>
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm"
      :size="formSize" status-icon style="margin-left: 300px;">
      <el-form-item label="用户名" prop="userName" style="width: 350px;">
        <el-input v-model="ruleForm.userName" />
      </el-form-item>
      <el-form-item label="性别" prop="name">
        <el-radio-group v-model="ruleForm.sex">
          <el-radio :value="false">男</el-radio>
          <el-radio :value="true">女</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="昵称" prop="nickName" style="width: 350px;">
        <el-input v-model="ruleForm.nickName" />
      </el-form-item>

      <el-form-item label="所在地址" prop="name">
        <el-cascader size="large" :options="options" v-model="provinces" @change="handleChange" placeholder="请选择省市区"
          style="width: 270px;" />
      </el-form-item>

      <el-form-item label="详细地址" prop="hamletName" style="width: 350px;">
        <el-input v-model="ruleForm.hamletName" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)" size="small">
          添加
        </el-button>

      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import axios from 'axios';
import { useRouter } from 'vue-router';
import { useCounterStore } from '@/stores/counter';
import { regionData } from "element-china-area-data";//引入
const apiUrl = import.meta.env.VITE_APP_API_URL;

const store = useCounterStore();
const router = useRouter();
const options = regionData;
console.log(options);

//#endregion


//#region
const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
//省市县储存
let provinces: any = reactive([])


const handleChange = (values: any) => {
  provinces = values
  console.log(provinces)
  ruleForm.provinceId = values[0]
  ruleForm.cityId = values[1]
  ruleForm.countyId = values[2]
}

const ruleForm: any = reactive({
  // "userId": 0,
  "userName": "",
  "password": "123456",

  "nickName": "",
  "sex": true,
  "provinceId": "",
  "cityId": "",
  "countyId": "",
  "hamletName": "",
  "createName": store.ccstore.nickName,
  "createTime": new Date(),
  "isDel": false
})


const rules = reactive<FormRules>({
  userName: [
    { required: true, message: '用户名不可为空', trigger: 'blur' },
  ],
  hamletName: [
    { required: true, message: '详细地址不可为空', trigger: 'blur' },
  ],
})

//添加
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      axios.post(apiUrl+'/T5/Message/AddUser', ruleForm).then(res => {
        if (res.data.code == 200) {
          ElMessage.success(res.data.msg);
          router.push({
            name: 'UserList'
          })
        } else {
          ElMessage.success("添加失败");
        }
      })
    } else {
      // console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
//#endregion

</script>
